package com.xxx.ware.ware_manage_sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.ware.ware_manage_sys.bean.dto.WebResultDto;
import com.xxx.ware.ware_manage_sys.bean.dto.WsDetailQueryDto;
import com.xxx.ware.ware_manage_sys.bean.dto.WsDetailUpdateSaveDto;
import com.xxx.ware.ware_manage_sys.bean.entity.warehouse.WmsWsDetail;
import com.xxx.ware.ware_manage_sys.bean.vo.WsDetailVo;
import com.xxx.ware.ware_manage_sys.mapper.WmsWsDetailMapper;
import com.xxx.ware.ware_manage_sys.service.IWsDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WsDetailServiceImpl extends ServiceImpl<WmsWsDetailMapper, WmsWsDetail> implements IWsDetailService {

    @Override
    public Boolean saveWs(WsDetailUpdateSaveDto wsDetailUpdateSaveDto) throws Exception {
        String warehouseName = wsDetailUpdateSaveDto.getWarehouseName();
        String remark = wsDetailUpdateSaveDto.getRemark();
        WmsWsDetail soleWare = getWareBySoleKey(warehouseName);
        if (soleWare != null) {
            throw new Exception("当前仓库已存在，对应仓库名称：" + warehouseName);
        }
        WmsWsDetail wmsWsDetail = new WmsWsDetail();
        wmsWsDetail.setWarehouseName(warehouseName);
        wmsWsDetail.setRemark(remark);
        return save(wmsWsDetail);
    }

    @Override
    public WmsWsDetail getWareBySoleKey(String warehouseName) {
        LambdaQueryWrapper<WmsWsDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WmsWsDetail::getWarehouseName, warehouseName);
        return this.getBaseMapper().selectOne(queryWrapper);
    }

    @Override
    public WebResultDto<WsDetailVo> getWareList(WsDetailQueryDto wsDetailQueryDto) {
        Integer pageNo = wsDetailQueryDto.getPageNo();
        Integer pageSize = wsDetailQueryDto.getPageSize();
        String warehouseName = wsDetailQueryDto.getWarehouseName();
        LambdaQueryWrapper<WmsWsDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WmsWsDetail::getStatus, 1);
        queryWrapper.orderByDesc(WmsWsDetail::getCreateTime);
        if (pageNo != null && pageSize != null) {
            log.info("仓库分页查询，入参：{}", wsDetailQueryDto);
            if (StringUtils.isNotBlank(warehouseName)) {
                queryWrapper.likeRight(WmsWsDetail::getWarehouseName, warehouseName);
            }
            IPage<WmsWsDetail> page = new Page<>(pageNo, pageSize);
            IPage<WmsWsDetail> resultPage = this.getBaseMapper().selectPage(page, queryWrapper);
            if (resultPage == null ||
                    resultPage.getRecords() == null ||
                    resultPage.getRecords().isEmpty()) {
                return WebResultDto.getDataList(pageNo, pageSize, resultPage.getTotal(), new ArrayList<>());
            }

            List<WsDetailVo> resultList = resultPage.getRecords().stream().map(e ->
                    {
                        WsDetailVo wsDetailVo = new WsDetailVo();
                        wsDetailVo.setId(e.getId());
                        wsDetailVo.setWarehouseName(e.getWarehouseName());
                        wsDetailVo.setStatus(e.getStatus().equals(1) ? "生效" : "失效");
                        wsDetailVo.setCreateTime(DateUtil.format(e.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                        wsDetailVo.setUpdateTime(DateUtil.format(e.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
                        wsDetailVo.setRemark(e.getRemark());
                        return wsDetailVo;
                    }
            ).collect(Collectors.toList());
            return WebResultDto.getDataList(pageNo, pageSize, resultPage.getTotal(), resultList);
        } else {//非分页查询情况
            log.info("仓库非分页查询，入参：{}", wsDetailQueryDto);
            queryWrapper.select(WmsWsDetail::getId, WmsWsDetail::getWarehouseName);
            List<WmsWsDetail> wmsWsDetails = this.getBaseMapper().selectList(queryWrapper);
            if (CollectionUtil.isEmpty(wmsWsDetails)) {
                return WebResultDto.getDataList(0, 0, 0L, new ArrayList<>());
            }
            List<WsDetailVo> resultList = wmsWsDetails.stream().map(e ->
                    {
                        WsDetailVo wsDetailVo = new WsDetailVo();
                        wsDetailVo.setId(e.getId());
                        wsDetailVo.setWarehouseName(e.getWarehouseName());
                        return wsDetailVo;
                    }
            ).collect(Collectors.toList());
            return WebResultDto.getDataList(0, 0, 0L, resultList);
        }
    }

    @Override
    public Boolean updateWare(WsDetailUpdateSaveDto wsDetailUpdateSaveDto) {
        WmsWsDetail wmsWsDetail = new WmsWsDetail();
        wmsWsDetail.setWarehouseName(wsDetailUpdateSaveDto.getWarehouseName());
        wmsWsDetail.setRemark(wsDetailUpdateSaveDto.getRemark());
        LambdaUpdateWrapper<WmsWsDetail> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(WmsWsDetail::getId, wsDetailUpdateSaveDto.getId());
        return update(wmsWsDetail, updateWrapper);
    }

    @Override
    public Boolean removeWare(Long id) {
        WmsWsDetail wmsWsDetail = new WmsWsDetail();
        wmsWsDetail.setId(id);
        wmsWsDetail.setStatus(2);
        return updateById(wmsWsDetail);
    }
}
